//
//  ConVipViewViewController.m
//  Shengzhuli
//
//  Created by subook on 2017/4/7.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//
#define space   ScreenW/7
#import "ConVipViewViewController.h"
#import "SZLAccountHelper.h"
#import "GFXNetworkHandle.h"
#import "SZLSinglePayViewController.h"
#import "SingleListModel.h"
#import "SZLSinglePayViewController.h"
#import "MBProgressHUD+MJ.h"
#import "VipSelectCell.h"
#import "VipTipsCell.h"
#import "VipNetwork.h"
#import "AdderMoneyModel.h"

@interface ConVipViewViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) NSInteger vipType;
@property (nonatomic,strong) NSArray *moneyArr;

@property (nonatomic,assign) NSInteger moneySelectType;
@property (nonatomic,strong) UIButton *payBtn;

@end

@implementation ConVipViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择身份";
    [self getAdderMoney];
    if (!self.type) {
        self.type = 3;
    }
    self.moneySelectType = 0;
    [self setUpTableView];
}
- (void)setUpTableView
{
    [self.view addSubview:self.tableView];
    UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    payBtn.frame = CGRectMake(20, ScreenH-64-20-40, ScreenW-40, 45);
    [payBtn setTitle:@"前往认证" forState:UIControlStateNormal];
    payBtn.backgroundColor = RGBColor(23, 146, 191);
    payBtn.layer.cornerRadius = 5.0;
    payBtn.clipsToBounds = YES;
    [payBtn addTarget:self action:@selector(payClick) forControlEvents:UIControlEventTouchUpInside];
    self.payBtn = payBtn;
    [self.view addSubview:payBtn];
    
    if ([SZLAccountHelper shareInstance].roleId == 2) {
        payBtn.hidden = YES;
    }else if ([SZLAccountHelper shareInstance].roleId == 2 && self.type != 2){
        payBtn.hidden = YES;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 160;
    }
    return 350;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        VipSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([VipSelectCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.type = self.type;
        cell.model = self.moneyArr[1];
        cell.selectBlock = ^(NSInteger type) {
            self.type = type;
            if (self.type == 3&&[SZLAccountHelper shareInstance].roleId == 3) {
                self.payBtn.hidden = YES;
            }else if (self.type == 2&&[SZLAccountHelper shareInstance].roleId == 3) {
                self.payBtn.hidden = NO;
            }
            [self.tableView reloadData];
        };
        return cell;
    }
    VipTipsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([VipTipsCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.moneyArr[indexPath.row];
    cell.type = self.type;
    cell.moneySelectBlock = ^(NSInteger type) {
        self.moneySelectType = type;
    };
    return cell;
}
- (void)payClick
{
    [self adderGoToPay];
}
- (void)getAdderMoney
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"token"] = [SZLAccountHelper shareInstance].token;
    [self showClearBgWaiter];
    [VipNetwork getVipMoneyWithParame:param response:^(BOOL success, NSString *msg, NSArray *moneyArr) {
        [self hideWaiter];
        if (success) {
            self.moneyArr = moneyArr;
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (void)adderGoToPay
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"token"] = [SZLAccountHelper shareInstance].token;
    param[@"memberType"] = @(self.type);
    if (self.type == 3) {
        AdderMoneyModel *model = self.moneyArr[0];
        param[@"amount"] = model.memberMoney;
    }else {
        if (self.moneySelectType == 0) {
            param[@"amount"] = @"20000";
        }else if (self.moneySelectType == 1) {
            param[@"amount"] = @"10000";
        }else if (self.moneySelectType == 2) {
            param[@"amount"] = @"5000";
        }else if (self.moneySelectType == 3) {
            param[@"amount"] = @"2000";
        }
    }
    [self showClearBgWaiter];
    [VipNetwork adderGotoPayWithParame:param response:^(BOOL success, NSString *msg,SingleCreateModel *sendModel) {
        [self hideWaiter];
        if (success) {
            SZLSinglePayViewController *szlPayVc = [[SZLSinglePayViewController alloc] init];
            szlPayVc.model = sendModel;
            szlPayVc.payChargeType = PayChargeTypeAdd;
            [self.navigationController pushViewController:szlPayVc animated:YES];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-64)];
        _tableView.dataSource = self;
        _tableView.delegate = self;self.tableView.backgroundColor = RGBColorOne(240);
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 65, 0);
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([VipSelectCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([VipSelectCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([VipTipsCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([VipTipsCell class])];
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

@end
